package com.wannengbang.storemobile.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.wannengbang.storemobile.R;
import com.wannengbang.storemobile.bean.StoreListBean;
import com.wannengbang.storemobile.homepage.adapter.SelectStoreListAdapter;

import java.io.Serializable;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Author:HWC
 */
public class SelectStoreDialog extends DialogFragment {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private Activity mActivity;
    private Dialog dialog;
    private List<StoreListBean.DataBean.ItemListBean> beanList;
    private OnCallBackListener onCallBackListener;

    public void setOnCallBackListener(OnCallBackListener onCallBackListener) {
        this.onCallBackListener = onCallBackListener;
    }

    private SelectStoreListAdapter listAdapter;
    private String store_no;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        initWindow();
        View view = inflater.inflate(R.layout.dialog_select_store, null);
        ButterKnife.bind(this, view);
        mActivity = getActivity();
        initView();
        return view;
    }


    public static SelectStoreDialog newInstance(List<StoreListBean.DataBean.ItemListBean> beanList, String store_no) {
        Bundle args = new Bundle();
        SelectStoreDialog fragment = new SelectStoreDialog();
        args.putSerializable("beanList", (Serializable) beanList);
        args.putString("store_no", store_no);
        fragment.setArguments(args);
        return fragment;
    }

    //    初始化请求
    private void initView() {
        beanList = (List<StoreListBean.DataBean.ItemListBean>) getArguments().getSerializable("beanList");
        store_no = getArguments().getString("store_no");
        recyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
        listAdapter = new SelectStoreListAdapter(beanList, store_no);
        recyclerView.setAdapter(listAdapter);

        listAdapter.setOnCallBackListener(position -> {
            if (onCallBackListener != null) {
                onCallBackListener.onCallBack(beanList.get(position).getName(), beanList.get(position).getStore_no());
            }
            dismiss();
        });
    }

    //设置window属性
    private void initWindow() {
        //去掉dialog的标题
        dialog = this.getDialog();
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setCancelable(true);
        Window window = dialog.getWindow();
        window.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.BOTTOM;
        window.getAttributes().windowAnimations = R.style.commomDialog;
        window.setAttributes(lp);
        window.setBackgroundDrawable(new ColorDrawable());
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }


    public interface OnCallBackListener {
        void onCallBack(String name, String select_store_no);
    }
}
